package com.rds.common.utils;

import com.rds.common.core.redis.RedisCache;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapUtils {

    /**
     * 将list转map
     */
    public static Map<String, String> listLessonSumByCourseIdList(List<Map<String,String>> list) {
        Map<String, String> map = new HashMap<>();
        if (list != null && !list.isEmpty()) {
            for (Map<String, String> map1 : list) {
                String key = null;
                String value = null;
                for (Map.Entry<String, String> entry : map1.entrySet()) {
                    if ("key".equals(entry.getKey())) {
                        key=String.valueOf(entry.getValue());
                    } else if ("value".equals(entry.getKey())) {
                        value = (String) entry.getValue();
                    }
                }
                map.put(key, value);
            }
        }
        return map;
    }

    /**
     * 类型、策略、状态转码
     * @param id 类型、策略、状态ID
     * @return 转码结果
     */
    public static String getTranscoding(RedisCache redisCache, Long id){
        String name="";
        Map<Long,String> typeList=redisCache.getCacheObject("typeList");
        Map<Long,String> policyList=redisCache.getCacheObject("policyList");
        Map<Long,String> stateList=redisCache.getCacheObject("stateList");
        String typeName=typeList.get(id);
        String policyName=policyList.get(id);
        String stateName=stateList.get(id);
        if(typeName!=null){
            name=typeName;
        }
        if(policyName!=null){
            name=policyName;
        }
        if(stateName!=null){
            name=stateName;
        }
        return name;
    }



    /**
     * map根据value获取key
     */
    public static <K, V> K getKey(Map<K, V> map, V value) {
        for (Map.Entry<K, V> entry : map.entrySet()) {
            if (entry.getValue().equals(value)) {
                return entry.getKey();
            }
        }
        return null;
    }
}
